﻿using System;
using System.Text.RegularExpressions;

namespace Utility
{
    /// <summary>
    /// 验证类
    /// </summary>
    public class RegexCheck
    {
        #region 基础验证类

        private static bool RegexStr(string Str, string RegStr)
        {
            bool boolstr = false;
            Regex reg = new Regex(RegStr, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            try
            {
                if (string.IsNullOrEmpty(Str) || !reg.IsMatch(Str))
                {
                    boolstr = false;
                }
                else
                {
                    boolstr = true;
                }
            }
            catch
            {
                boolstr = false;
            }
            return boolstr;
        }

        #endregion 基础验证类

        /// <summary>
        /// 防sql入侵
        /// </summary>
        /// <param name="sInput"></param>
        /// <returns></returns>
        public static string Filter(string sInput)
        {
            if (sInput == null || string.IsNullOrEmpty(sInput))
                return null;
            string sInput1 = sInput.ToLower();
            string output = sInput;
            string pattern = @"*|AND|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
            if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
            {
                throw new Exception("字符串中含有非法字符!");
            }
            else
            {
                output = output.Replace("'", "''");
            }
            return output;
        }

        /// <summary>
        /// 正则判断是否整数
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static bool RegexInt(string Str)
        {
            bool boolstr = false;
            Regex reg = new Regex(@"^\d+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            try
            {
                if (string.IsNullOrEmpty(Str) || !reg.IsMatch(Str))
                {
                    boolstr = false;
                }
                else
                {
                    boolstr = true;
                }
            }
            catch
            {
                boolstr = false;
            }
            return boolstr;
        }

        /// <summary>
        /// 是否为数字
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static bool RegexNumber(string Str)
        {
            bool boolstr = false;
            Regex reg = new Regex(@"^[-+]?\d*[\.]?\d*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            try
            {
                if (string.IsNullOrEmpty(Str) || !reg.IsMatch(Str))
                {
                    boolstr = false;
                }
                else
                {
                    boolstr = true;
                }
            }
            catch
            {
                boolstr = false;
            }
            return boolstr;
        }

        /// <summary>
        /// 是否为日期
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static bool IsDateTime(string Str)
        {
            bool boolstr = false;
            if (string.IsNullOrEmpty(Str))
            {
                boolstr = false;
            }
            else
            {
                try
                {
                    DateTime.Parse(Str);
                    boolstr = true;
                }
                catch
                {
                    boolstr = false;
                }
            }
            return boolstr;
        }

        /// <summary>
        /// 是否为正整数
        /// </summary>
        /// <remarks></remarks>
        public static bool IsInt(string str)
        {
            return RegexStr(str, @"^\d+$");
        }

        /// <summary>
        /// 是否为正数字
        /// </summary>
        /// <remarks></remarks>
        public static bool IsNumer(string str)
        {
            return RegexStr(str, @"^[-+]?\d*[\.]?\d*$");
        }

        /// <summary>
        /// 是否为名字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsUserName(string str)
        {
            return RegexStr(str, @"^([a-zA-Z])[0-9_a-zA-Z]{4,19}$");
        }

        /// <summary>
        /// 是否为钱
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMoney(string str)
        {
            return RegexStr(str, @"^\d*[\.]?(\d{0,2})?$");
        }

        /// <summary>
        /// 是否为IDS如(1,2|1)
        /// </summary>
        /// <remarks></remarks>
        public static bool IsIds(string str)
        {
            return RegexStr(str, @"^[\d]*(,[\d]*)*$");
        }

        /// <summary>
        /// 是否为英文字母组合
        /// </summary>
        /// <remarks></remarks>
        public static bool IsLetter(string str)
        {
            return RegexStr(str, @"^[a-zA-Z]*$");
        }

        /// <summary>
        /// Email
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEmail(string str)
        {
            return RegexStr(str, @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
        }

        /// <summary>
        /// Url
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsUrl(string str)
        {
            return RegexStr(str, @"^(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?");
        }

        /// <summary>
        /// IP
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIP(string str)
        {
            return RegexStr(str, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
        }

        /// <summary>
        /// 中文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsChinese(string str)
        {
            return RegexStr(str, @"^[\u4e00-\u9fa5]$");
        }

        /// <summary>
        /// 中文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool HasChinese(string str)
        {
            Regex regex = new Regex("^[\u4e00-\u9fa5]$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            bool ISchinese = false;
            char[] noWChar = str.ToCharArray();
            for (int j = 0; j < noWChar.Length; j++)
            {
                // 中文字符
                if (regex.IsMatch(noWChar[j].ToString()))
                {
                    ISchinese = true;
                    break;
                }
                // 非中文字符
            }
            return ISchinese;
        }

        /// <summary>
        /// 电话
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsTel(string str)
        {
            return RegexStr(str, @"^[0-9]{3,4}\-[0-9]{7,8}$");
        }

        /// <summary>
        /// 手机
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMob(string str)
        {
            return RegexStr(str, "^(0)?(13|15|18)[0-9]{9}$");
        }
    }
}